document.write("<script type='text/javascript' src='/lib/dc/request.js'></script>");
document.write("<script type='text/javascript' src='/lib/dc/util.js'></script>");
document.write("<script type='text/javascript' src='/lib/jquery.min.js'></script>");
 /**
  * 分页对象
  * @author lcg
  * @date 2019/10/17
  * @param e 代表 dom this
  **/
class PageHelper {

    p_result = {data:[],example: null,limit: null,msg: null,page: 1 ,status: 0,total: 0,totalPage:0};//Result对象    私有
    p_limit = 10;//限制条数  私有
    p_key = '';//关键词      私有
    p_cur = 1;//当前页     私有
    p_skip = 1;//跳页      私有
    p_checkeds = [];//删除的id列表*/    私有
    url = '';//请求页表的url
    e_limit =''; //dom元素的id或者class，绑定限制页数
    e_total ='';//dom元素的id或者class，绑定所有条数
    e_skip ='';//dom元素的id或者class，绑定跳页
    e_select ='';//dom元素的id或者class，绑定单选
    e_select_all = '';//dom元素的id或者class，绑定全选
    e_content = '';//dom元素的id或者class，绑定列表容器
    f_edit;        //编辑的方法 todo 需要重写   参数为dom this   function(this)
    f_detail;      //详情的方法 todo 需要重写   参数为dom this   function(this)
    f_delete;      //删除的方法 todo 需要重写   参数为dom this   function(this)
    request_params = null; //请求参数 私有
    obj_cur_action = null; //当前编辑，删除，详情对象    私有

     /**
      *
      * @param url  必填
      * @param e_content    必填
      * @param callback      必填
      * @param e_limit     必填
      * @param e_total      必填
      * @param e_skip       必填
      * @param e_select      可选
      * @param e_select_all  可选
      */
    constructor(url,e_content,callback,e_limit,e_total,e_skip,e_select,e_select_all){
        this.url = url;
        this.e_content = e_content;
        this.setHtml = callback;
        this.e_limit = e_limit;
        this.e_total = e_total;
        this.e_skip = e_skip;
        this.e_select = e_select;
        this.e_select_all = e_select_all;
        this.p_result.limit = this.p_limit; //初始化默认限制
    }
    
    /*刷新页码*/

    refreshPage() {
        $(this.e_limit).text(this.p_result.limit);
        $(this.e_total).text(this.p_result.total);
        $(this.e_skip).val(this.p_result.page);
        this.p_limit = this.p_result.limit;
        this.p_cur = this.p_result.page;
        this.p_skip = this.p_result.page;
        if (this.p_checkeds.length <= 0)
            $(this.e_select_all).prop('checked',false);
    }


    /*渲染列表函数*/
    getInfo(skip) {
        let _this = this;
        _this.p_limit = _this.p_result.limit;  //获取返回的
        let cur = this.p_cur;
        if (!isNaN(skip))
            cur = skip;
        this.p_skip = cur;
        let data;
        if (_this.request_params){  //自定义参数
            data = _this.request_params;
            data.page = _this.p_cur;
            let limit = _mapGetValue("limit",_this.request_params);
            if (limit && limit != '' && !isNaN(limit))
                _this.p_limit = limit;
            else
                console.error("limit 必须是数字类型");
        }else {
            data = {page: cur, limit: _this.p_limit, key: _this.p_key};   //默认参数
        }
        console.warn("PageHelper请求的参数："+JSON.stringify(data))
        POST(this.url, data, function (data) {
            let res = data;
            console.warn("PageHelper响应的结果")
            console.warn(res)
            _this.p_result = res;   //response
            _this.refreshPage();
            let $e_content = $(_this.e_content);
            if ($e_content.length <= 0)
                console.error("找不到容器")
            $e_content.empty();
            let list = res.data;
            if (!list)
                return;
            list.forEach(function (item, index) {
                $(_this.setHtml(_this,item,index)).appendTo($e_content);
            });
        });

    }
    /* must return a html string*/
    setHtml(curObj,item,index){
        //todo must overwrite
        //构造方法时 参数一个都不能少
    }


    /*设置请求参数接口*/
    set_request_params(param_obj){
        this.request_params = param_obj;
    }

    get_obj_cur_action(){
        return this.obj_cur_action; //当前编辑，删除，详情对象
    }

    get_checkeds(){   //获取input 选中
        return this.p_checkeds;
    }

    _detail(e){
        let id = $(e).attr('value');   //需要给标签赋值
        let index = $(e).attr('index'); //需要给标签赋值
        this.cur_row_obj(index);
        this.f_detail(id);
        this.log_option_row(index)

    }

    _edit(e){
        let id = $(e).attr('value');  //需要给标签赋值
        let index = $(e).attr('index');   //需要给标签赋值
        this.cur_row_obj(index);
        this.f_edit(id);
        this.log_option_row(index)
    }

    _delete(e){
        let id = $(e).attr('value'); //需要给标签赋值
        let index = $(e).attr('index'); //需要给标签赋值
        this.cur_row_obj(index);
        this.f_delete(id);
        this.log_option_row(index)
    }

    log_option_row(index){
        console.warn("PageHelper CurrentRow: "+index)
        console.warn("PageHelper CurrentRow data: ")
        console.log(this.obj_cur_action)
    }

    cur_row_obj(index){
        if (index.trim()!=''  && !isNaN(index) && index >= 0 &&index <= this.p_result.data.length -1)
            this.obj_cur_action = this.p_result.data[index];
    }


   /*刷新当前页*/
    refreshData(){
        this.getInfo();
        this.p_checkeds.splice(0);//清除选中的id
    }

    /*上一页*/
    lastPage() {
        if (this.p_cur <= 1){
            alert("当前为第一页");
            return;
        }
        this.p_cur --;
        this.getInfo();
        this.resetSelect();
    }

    /*下一页*/

    nextPage() {
        if (this.p_cur >= this.p_result.totalPage){
            alert("当前为最后一页");
            return;
        }
        this.p_cur ++;
        this.getInfo();
        this.resetSelect();
    }

    /*跳页*/

    skipPage(e) {
        let page = $(e).val();
        this.p_skip = page;
        this.p_cur =page;
        if (isNaN(this.p_skip)){
            alert("请正确输入页数");
            return;
        } else if (this.p_skip < 1){
            alert("页数不能小于1");
            return;
        }else if (this.p_skip > this.p_result.totalPage){
            alert("页数不能超过"+this.p_result.totalPage);
            return;
        }
        this.resetSelect();
        this.getInfo(this.p_skip);
    }

    /*搜索方法*/


    search(e) {
        this.p_cur = 1;
        this.p_key = $(e).val();
        this.getInfo();
        this.refreshPage();
    }

    /*选择一个input*/
    selected(e) {

        let $p_select_all = $(this.e_select_all);
        let el = $(e);
        let check = el.prop('checked');
        let val = el.val();
        if (e.type == 'radio'){
            this.p_checkeds.splice(0);
            this.p_checkeds.push(val)
        } else{
            if (check)
                arrayPush(this.p_checkeds,val);
            else
                arrayPop(this.p_checkeds,val);
            if (this.p_checkeds.length >= this.p_result.data.length)
                $p_select_all.prop('checked',true);
            else
                $p_select_all.prop('checked',false);
        }

        console.log(this.p_checkeds);
    }

    /*选择全部checkbox ，key 不传默认使用id
    * */

    selectedAll(e,key) {
        
        let $p_select = $(this.e_select);
        let checked = $(e).prop('checked');
        if (checked){
            let list = this.p_result.data;
            for (let i = 0; i < list.length; i++) {
                if (key && key != ''){
                    this.arrayPush(this.p_checkeds,_mapGetValue(key,list[i]));
                }else {
                    this.arrayPush(this.p_checkeds,list[i].id);//item.id 根据实际字段  默认id
                }
            }
            $p_select.each(function (i,e) {
                $(e).prop('checked',true);
            })
        }else{
            this.p_checkeds.splice(0);
            $p_select.each(function (i,e) {
                $(e).prop('checked',false);
            })
        }
        console.log(this.p_checkeds);
    }

    /*删除勾选*/
    delAllSelect(url) {
        let _this = this;
        if (confirm("确定要删除吗？")){
            POST(url,{ids:this.p_checkeds.toString()},function (data) {
                _this.Alert(data);
                _this.refreshData();
            })
        }
    }

    /*提醒框*/
    Alert(data) {
        alert(data.massage);
    }

    /*重置选中状态*/
    resetSelect() {
        let $p_select = $(this.e_select_all);
        $p_select.prop('checked',false);
        this.p_checkeds.splice(0);
    }

    /**
     * 获取选中的数组
     */

    getCheckeds(){
        return this.p_checkeds;
    }


    /**
     * 数组push去重
     * */
    arrayPush(targetArr,val){
        if (!isArray(targetArr))
            return;
        if (targetArr.indexOf(val) === -1)
            targetArr.push(val);
        return targetArr;
    }

    arrayPop(targetArr,val) {
        let index = -1;
        if (!isArray(targetArr))
            return;
        index = targetArr.indexOf(val);
        if ( index !== -1)
            targetArr.splice(index,1);
        return targetArr;
    }
}


